package com.ruoyi.orders.service.impl;

import java.util.Date;
import java.util.List;

import cn.hutool.core.date.DateUnit;
import com.ruoyi.book.domain.Book;
import com.ruoyi.book.mapper.BookMapper;
import com.ruoyi.ordersItem.domain.OrdersItem;
import com.ruoyi.ordersItem.mapper.OrdersItemMapper;
import cn.hutool.core.date.DateUtil;
import com.ruoyi.user1.domain.User;
import com.ruoyi.user1.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.orders.mapper.OrdersMapper;
import com.ruoyi.orders.domain.Orders;
import com.ruoyi.orders.service.IOrdersService;

/**
 * 订单信息Service业务层处理
 *
 * @author ruoyi
 * @date 2025-01-21
 */
@Service
public class OrdersServiceImpl implements IOrdersService
{
    @Autowired
    private OrdersMapper ordersMapper;

    @Autowired
    private OrdersItemMapper ordersItemMapper;

    @Autowired
    private BookMapper bookMapper;

    @Autowired
    private IUserService userService;

    /**
     * 查询订单信息
     *
     * @param id 订单信息主键
     * @return 订单信息
     */
    @Override
    public Orders selectOrdersById(Long id)
    {
        return ordersMapper.selectOrdersById(id);
    }

    /**
     * 查询订单信息列表
     *
     * @param orders 订单信息
     * @return 订单信息
     */
    @Override
    public List<Orders> selectOrdersList(Orders orders)
    {
        return ordersMapper.selectOrdersList(orders);
    }

    /**
     * 新增订单信息
     *
     * @param orders 订单信息
     * @return 结果
     */
    @Override
    public int insertOrders(Orders orders)
    {
        return ordersMapper.insertOrders(orders);
    }

    /**
     * 修改订单信息
     *
     * @param orders 订单信息
     * @return 结果
     */
    @Override
    public int updateOrders(Orders orders)
    {
        return ordersMapper.updateOrders(orders);
    }

    /**
     * 批量删除订单信息
     *
     * @param ids 需要删除的订单信息主键
     * @return 结果
     */
    @Override
    public int deleteOrdersByIds(Long[] ids)
    {
        return ordersMapper.deleteOrdersByIds(ids);
    }

    /**
     * 删除订单信息信息
     *
     * @param id 订单信息主键
     * @return 结果
     */
    @Override
    public int deleteOrdersById(Long id)
    {
        return ordersMapper.deleteOrdersById(id);
    }

    /**
     * 修改订单状态
     * @param id
     * @return
     */
    @Override
    public int changeStatus(Long id) {
       Orders orders = ordersMapper.selectOrdersById(id);
       String status =orders.getStatus();
       if("借书待确认".equals(status)){
           orders.setStatus("已借出");
           List<OrdersItem> ordersItems = ordersItemMapper.selectByOrderId(id);
           for (OrdersItem ordersItem : ordersItems) {  // 查询本次借的所有图书  按个扣减库存
               Long bookId = ordersItem.getBookId();
               Book book = bookMapper.selectBookById(bookId);
               Long num = ordersItem.getNum();  // 借书的数量
               book.setStore(book.getStore() - num);
               bookMapper.updateBook(book);  // 更新图书库存
           }
       }
        if("还书待确认".equals(status)){
            orders.setStatus("已归还");
            List<OrdersItem> ordersItems = ordersItemMapper.selectByOrderId(id);
            for (OrdersItem ordersItem : ordersItems) {  // 查询本次借的所有图书  按个扣减库存
                Long bookId = ordersItem.getBookId();
                Book book = bookMapper.selectBookById(bookId);
                Long num = ordersItem.getNum();  // 借书的数量
                book.setStore(book.getStore() + num);
                bookMapper.updateBook(book);  // 更新图书库存

                // 更新信誉分  当借出的天数超过30天  就会扣除信誉分
                String time = orders.getTime();
                long between = 1 + DateUtil.between(DateUtil.parseDate(time), new Date(), DateUnit.DAY);// 计算借书的时长（天）
                if (between > 30) {
                    Long userId = orders.getUserId();
                    User user = userService.selectUserById(userId);
                    user.setScore(user.getScore() - 10);
                    userService.updateUser(user);
                }
            }
        }
        return ordersMapper.updateOrders(orders);
    }
}
